<?php
namespace addons\caipiao\controller;

use addons\caipiao\service\CplistService;
use addons\caipiao\service\HomeService;
use addons\caipiao\validate\HomeValidate;
use app\common\controller\Addon;
use think\facade\Log;
use think\facade\Request;

class Home extends Addon{
    public $isWexinLogin=true;//为 TRUE 时，代表是需要微信授权登录，获取会员信息
    public function index(){
        $m = getMember();
        if(Request::instance()->isPost()){
            $openid = $m['openid'];
            $relname = Request::put('relname','');
            $mobile = Request::put('mobile','');
            $img_url = Request::put('img_url','');
            $cplistS = new CplistService();
            $data = [
                'relname'=>$relname,
                'mobile'=>$mobile,
                'img_url'=>$img_url,
                'openid'=>$openid,
                'balance'=>0
            ];
            $v = new HomeValidate();
            if(!$v->scene('add')->check($data)){
                return json(['code'=>1,'msg'=>$v->getError()]);
            }
            $r = $cplistS->add($data);
            if($r){
                return json(['code'=>0,'msg'=>'添加成功']);
            }else{
                return json(['code'=>1,'msg'=>'添加失败']);
            }
        }else{
            $relname = $m['relname'];
            $mobile = $m['mobile'];
            if(uploadQiniuType($m['id'])){
                $uploadUrl = url('mp/Upload/qiniuUpload');
            }else{
                $uploadUrl = url('mp/Upload/uploadImg');
            }
            $this->assign('relname',$relname);
            $this->assign('mobile',$mobile);
            $this->assign('uploadUrl',getHostDomain().$uploadUrl);
            return $this->fetch();
        }
    }

    public function pagelist(){
        $m = getMember();
        $openid = $m['openid'];
        if(Request::instance()->isPost()){
            $page = Request::put('page',1,'int');
            $limit = Request::put('limit',10,'int');
            $homeS = new HomeService();
            try{
                $data = $homeS->lists($openid,$page,$limit);
                return json(['code'=>0,'msg'=>'获取列表数据成功','data'=>$data]);
            }catch (\Exception $exception){
                return json(['code'=>1,'msg'=>$exception->getMessage(),'data'=>[]]);
            }
        }else{
            return $this->fetch();
        }
    }
    //帮助页面
    public function help(){
        return $this->fetch();
    }
}
